package com.olddriver.crm.service.impl;

import com.olddriver.crm.domain.*;
import com.olddriver.crm.mapper.ClientFailMapper;
import com.olddriver.crm.mapper.ClientLoseMapper;
import com.olddriver.crm.mapper.ClientMapper;
import com.olddriver.crm.mapper.ClientMoveMapper;
import com.olddriver.crm.page.PageResult;
import com.olddriver.crm.query.ClientQueryObject;
import com.olddriver.crm.service.IClientService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class ClientServiceImpl implements IClientService{

    @Autowired
    private ClientMapper clientMapper;
    @Autowired
    private ClientMoveMapper clientMoveMapper;
    @Autowired
    private ClientFailMapper clientFailMapper;
    @Autowired
    private ClientLoseMapper clientLoseMapper;

    public int save(Client client){
        //设置录入时间
        client.setInputTime(new Date());
        Employee inputUser = (Employee) SecurityUtils.getSubject().getPrincipal();
        //设置录入人
        client.setInputUser(inputUser);
        return clientMapper.save(client);
    }

    public void updateStatus(Long id,Byte status,String reason) {
        //改变指定的客户的状态码
        clientMapper.updateStatus(id,status);
        //如果是将客户的状态码设置为开发失败，则记录到开发失败的表中
        if(new Byte("4").equals(status)){
            //记录到开发失败的表中
            Client client = clientMapper.get(id);
            ClientFail clientFail = new ClientFail();
            clientFail.setInputTime(new Date());
            clientFail.setName(client.getName());
            clientFail.setReason(reason);
            clientFail.setTakeUser(client.getTakeUser());
            clientFailMapper.save(clientFail);
        }
        //如果是把客户的状态码设置为潜在客户时(吸纳)，则清空takeUser
        if(new Byte("0").equals(status)){
            Client client = new Client();
            client.setId(id);
            clientMapper.updateTakeUser(client);
        }
        //如果是把客户的状态吗设置为流失客户时，则记录到客户流失表中
        if(new Byte("5").equals(status)){
            Client client = clientMapper.get(id);
            ClientLose clientLose = new ClientLose();
            clientLose.setInputTime(new Date());
            clientLose.setName(client.getName());
            clientLose.setReason(reason);
            clientLose.setTakeUser(client.getTakeUser());
            clientLoseMapper.save(clientLose);
        }
    }

    public void update(Client client){
        clientMapper.update(client);
    }

    public void delete(Long id) {
        clientMapper.delete(id);
    }

    public Client get(Long id) {
        return clientMapper.get(id);
    }

    public List<Client> list() {
        return clientMapper.list();
    }

    public PageResult query(ClientQueryObject qo) {
        int total = clientMapper.queryTotal(qo);
        if (total == 0) {
            return new PageResult(total, Collections.emptyList());
        }
        return new PageResult(total, clientMapper.queryRows(qo));
    }

    //客户的移交
    public void updateTakeUser(Client client,String reason) {
        Employee oldTakeUser = clientMapper.get(client.getId()).getTakeUser();
        Client oldClient = clientMapper.get(client.getId());
        ClientMove clientMove = new ClientMove();
        //获取当前操作人
        Employee inputUser = (Employee) SecurityUtils.getSubject().getPrincipal();

        clientMove.setInputUser(inputUser);
        clientMove.setInputTime(new Date());
        clientMove.setReason(reason);
        clientMove.setName(oldClient.getName());
        clientMove.setOldTakeUser(oldTakeUser);

        Employee employee = new Employee();
        employee.setId(client.getTakeUser().getId());

        clientMove.setNewTakeUser(employee);

        clientMoveMapper.save(clientMove);
        clientMapper.updateTakeUser(client);
    }

    public List<Client> selectNoTakeUser(ClientQueryObject qo) {
        return clientMapper.selectNoTakeUser(qo);
    }

    public int getUnsignClient(){
        return clientMapper.getUnsignCount();
    }

}
